// 火焰弹 贝塞尔曲线
// @author:wangff8@qq.com  @date:2024-10-31
library SimulateAttac3 initializer init requires SimulateAttack

    private function doMove takes nothing returns nothing
        
        local timer tm = GetExpiredTimer()
        local integer tid = GetHandleId(tm)
        local effect efc = LoadEffectHandle(gameht, tid, 2)
        local real jd = LoadReal(gameht, tid, 3)
        local integer uid = LoadInteger(gameht, tid, 15)

        // 怪物死亡点
        local real x0 = LoadReal(gameht, tid, 10)
        local real y0 = LoadReal(gameht, tid, 11)
        local real z0 = getZByXY(x0, y0)

        // 目标位置
        local unit bu = LoadUnitHandle(gameht, tid, 12)
        local real x2 = GetUnitX(bu)
        local real y2 = GetUnitY(bu)
        local real z2 = getZByXY(x2, y2)

        // 控制点
        local real x1 = CosBJ(jd) * 1000.0 + x0
        local real y1 = SinBJ(jd) * 1000.0 + y0
        local real z1 = z0 + 350.0

        local real x
        local real y
        local real z
        local real t = LoadReal(gameht, tid, 1) + 0.01
        call SaveReal(gameht, tid, 1, t)
        
        // 刷新上一次移动的位置
        if x2 != 0.0 or y2 != 0.0 then
            call SaveReal(gameht, tid, 20, x2)
            call SaveReal(gameht, tid, 21, y2)
        endif

        if x2 == 0.0 and y2 == 0.0 then
            set x2 = LoadReal(gameht, tid, 20)
            set y2 = LoadReal(gameht, tid, 21)
            set z2 = getZByXY(x2, y2)
        endif

        // 到位置结束
        if (t >= 1.0) then
            call DestroyEffect(efc)
            call FlushChildHashtable(gameht, tid)
            call DestroyTimer(tm)

            call UnitDamageTarget( udg_player_select_hero[uid], bu, 200.0, true, true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS )
            // call DestroyEffect(AddSpecialEffect("effect_tx_asad.mdl", x2, y2))
            // call damageByHYY(uid, x2, y2)
        endif

        set x = Pow(1.0 - t, 2.0)*x0 + 2.0*(1.0 - t)*t*x1 + Pow(t, 2.0)*x2
        set y = Pow(1.0 - t, 2.0)*y0 + 2.0*(1.0 - t)*t*y1 + Pow(t, 2.0)*y2
        set z = Pow(1.0 - t, 2.0)*z0 + 2.0*(1.0 - t)*t*z1 + Pow(t, 2.0)*z2
        call EXSetEffectXY( efc, x, y )
        // set z = getZByXY(x, y) + z
        // call BJDebugMsg(" = z = " + R2S(z))
        call EXSetEffectZ( efc, z )
        // call EXSetEffectZ( efc, getZByXY(x, y) )

        set efc = null
        set tm = null
        set bu = null
    endfunction
    // 释放火焰弹
    function releseHYD takes integer uid returns nothing
        local unit u = udg_player_select_hero[uid]
        local unit bu = getNearestUnit(uid)
        local string ddmdl = "tsw_huoyd.mdx"
        local timer t
        local integer tid
        local real x0 = GetUnitX(u)
        local real y0 = GetUnitY(u)
        local real x2 = GetUnitX(bu)
        local real y2 = GetUnitY(bu)

        local effect efc
        local integer r
        local real jd = getAngleBetweenXY(x0, y0, x2, y2)

        // 如果1000范围内有单位则攻击最近的，否则随机攻击
        if getDistance(GetUnitX(u), GetUnitY(u), GetUnitX(bu), GetUnitY(bu)) > 1000.0 then
            set bu = getRandomUnit(uid)
            set x2 = GetUnitX(bu)
            set y2 = GetUnitY(bu)

            set jd = getAngleBetweenXY(x0, y0, x2, y2)
        endif

        set t = CreateTimer()
        set tid = GetHandleId(t)

        set efc = AddSpecialEffect(ddmdl, x0, y0)
        // call EXSetEffectZ( efc, 0.00 )
        call SaveEffectHandle(gameht, tid, 2, efc)

        set jd = jd + 180.0
        
        if GetRandomInt(1, 2) == 1 then
            set jd = jd + I2R(GetRandomInt(1, 4) * 10 * -1)
        else
            set jd = jd + I2R(GetRandomInt(1, 4) * 10 * 1)
        endif
        // call DisplayTextToPlayer(Player(0), 0, 0, "jd - " + R2S(jd))
        call SaveReal(gameht, tid, 3, jd)
        call SaveReal(gameht, tid, 10, x0)
        call SaveReal(gameht, tid, 11, y0)
        call SaveUnitHandle(gameht, tid, 12, bu)
        call SaveInteger(gameht, tid, 15, uid)
        call SaveReal(gameht, tid, 20, x2)
        call SaveReal(gameht, tid, 21, y2)

        call TimerStart(t, 0.01, true, function doMove)

        set efc = null
        set t = null
    endfunction

    // 释放火焰弹
    // function releseHYD takes integer uid returns nothing
    //     local unit u = udg_player_select_hero[uid]
    //     local unit bu = getNearestUnit(uid)
    //     local string ddmdl = "tsw_huoyd.mdx"
    //     local real shv = 180.0
    //     local real height = 80.0
        
    //     call relHuoYD(u, bu, ddmdl, shv, height)

    // endfunction

    private function init takes nothing returns nothing
        
    endfunction
endlibrary



